package com.zito.health.api.controller;

import com.zito.health.api.annotation.AppGetMapping;
import com.zito.health.api.annotation.AppPostMapping;
import com.zito.health.api.result.ResponseBean;
import com.zito.health.entity.User;
import com.zito.health.service.UserService;
import com.zito.health.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/app")
public class LoginAppController {

    @Autowired
    UserService userService;


    @PostMapping("/login")
    public ResponseEntity<ResponseBean> login(User user) {

        String msg= "登录成功";
        Integer code=200;
        User u =new User();
        u.setUsername(user.getUsername());
        u.setEnabled(1);//可用
        u = userService.queryOne(u);
        if(u!=null){
           //科研账号(app不能登科研的帐号)
            Integer[] ky={4,5,8,10};
            for(int i=0;i<ky.length;i++){
                if(ky[i] == u.getRoleId()){
                    msg = "没有权限";
                    code = HttpStatus.NOT_ACCEPTABLE.value();
                }
            }
        }else{
            msg = "账号不存在或密码错误";
            code = HttpStatus.NOT_ACCEPTABLE.value();
        }

        if(code == 200){
            HashMap<String, Object> output = userService.exchangeToken(user.getUsername(), user.getPassword());
            return new ResponseEntity(
                    new ResponseBean(code, msg ,output),
                    new HttpHeaders() {{
                        add(HttpHeaders.AUTHORIZATION, "APP-TOKEN " + output.get("token"));
                    }},
                    HttpStatus.OK
            );
        }else{
            return new ResponseEntity(
                    new ResponseBean(code, msg ,null),
                    HttpStatus.OK
            );
        }



    }

    @AppGetMapping(value = "/aaa")
    public ResponseBean test (){
        System.out.println("未涉深山，不知道路之美");
        return new ResponseBean(200,"未见沧海，不知天之大");
    }

    @AppPostMapping(value = "/copy")
    public ResponseBean copy (){
        System.out.println("时光时光慢些吧");
        return new ResponseBean(200,"逝去的青春");
    }

    @GetMapping("/demo")
    public ResponseBean demo (){
        System.out.println("没有拦截你吧");
        return new ResponseBean(200,"adidas one");
    }



}
